//
//  Cook_SecViewController.m
//  Cook Street
//
//  Created by dllo on 15/8/8.
//  Copyright (c) 2015年 LANOU. All rights reserved.
//

#import "Cook_SecViewController.h"
#import "AFN.h"
#define WIDTH ([UIScreen mainScreen].applicationFrame.size.width)
#define HEIGHT ([UIScreen mainScreen].applicationFrame.size.height)
#import "Cook_SecCollectionViewCell.h"
#import "CookModel.h"
#import "Cook_Thir_ViewController.h"

@interface Cook_SecViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, strong) NSMutableArray *array;
@property (nonatomic, strong) NSMutableArray *secArray;

//************************************

@end

@implementation Cook_SecViewController

#pragma mark - 懒加载数据重写get方法
- (NSMutableArray *)secArray
{
    if (_secArray == nil) {
        
        NSMutableArray *array = [NSMutableArray arrayWithCapacity:0];
        
        _secArray = array;
    }
    return _secArray;
}

- (NSMutableArray *)array
{
    if (_array == nil) {
        
        NSMutableArray *array = [NSMutableArray arrayWithCapacity:0];
        _array = array;
    }
    return _array;
}

// 隐藏tabbar
- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = self.name;
    
    _flowLayout = [[UICollectionViewFlowLayout alloc]init];
    _flowLayout.itemSize = CGSizeMake((WIDTH - 60) / 2, (WIDTH - 60) / 2 );
    _flowLayout.minimumLineSpacing = 30;
    _flowLayout.sectionInset = UIEdgeInsetsMake(20, 20, 80, 20);
    
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) collectionViewLayout:_flowLayout];
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor clearColor];
    [_collectionView registerClass:[Cook_SecCollectionViewCell class] forCellWithReuseIdentifier:@"reuseId"];
    [self.view addSubview:_collectionView];
    
    
    [self GET];
    
    UIButton * leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 30, 20);
    [leftButton addTarget:self action:@selector(leftClick) forControlEvents:UIControlEventTouchUpInside];
    [leftButton setBackgroundImage:[UIImage imageNamed:@"backk.png"] forState:UIControlStateNormal];
    UIBarButtonItem * custom = [[UIBarButtonItem alloc]initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = custom;
    
}


-(void)GET
{
    
    [AFN urlString:[NSString stringWithFormat:@"http://apis.juhe.cn/cook/index? key=1a598cdd68c9b10550d6403b5db054e6&dtype=json&cid=%@&pn=&rn=&format=",self.num] getAFNdata:^(id responseObject) {
        
        NSMutableDictionary *resultDic = [responseObject objectForKey:@"result"];
        NSMutableArray *dataArray = [resultDic objectForKey:@"data"];
        for (NSMutableDictionary *dic in dataArray) {            
            
            CookModel *cook = [[CookModel alloc]init];
            [cook setValuesForKeysWithDictionary:dic];

            for (NSString *str in cook.albums) {
                
                cook.DBpic = str;
            }
            cook.name = self.name;
            [self.array addObject:cook];

     }
        [self.collectionView reloadData];
    }];
    
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.array.count;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    Cook_SecCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuseId" forIndexPath:indexPath];
    CookModel *mo = self.array[indexPath.row];
    [cell get:mo];
    return cell;
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    Cook_Thir_ViewController *thir = [[Cook_Thir_ViewController alloc]init];
    [self.navigationController pushViewController:thir animated:YES];
    
    thir.num = self.num;
    
    CookModel *model = [self.array objectAtIndex:indexPath.row];
    
    thir.model = model;
    
    thir.thir_id = model.cid;
    
    thir.title = model.title;         //传菜名
    
    thir.tag = model.tags;            //传标记

    thir.ing = model.ingredients;     //传主料
    
    thir.bur = model.burden;         //传辅料
    
    thir.stepArr = model.steps;
    
    thir.pic = model.DBpic;   //传图片
    
}




-(void)leftClick
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
